//封装请求是否自动带上token
const API_BASE_URL = 'http://localhost:8080';

export function Login(username, password, code, uuid){
    const data = {
        username,
        password,
        code,
        uuid
      }
    wx.request({
      url: API_BASE_URL+ '/login',
      method:'POST',
      headers:{ isToken:false },
      data:data,
      success(res){
          var data = res.data;
          console.log(data.code == "200");
          if(data.code == "200"){
              console.log(data)
              wx.setStorageSync('token', data.token);
              wx.redirectTo({
                url: '../index/index',
              })
          }
      }
    })
}
export function GetUserInfo(){

}

export const request = (url, method, data = {}) => {
  return new Promise((resolve, reject) => {
    var token =  wx.getStorageSync('token');
    console.log(token);
    var headers = {};
    headers['Content-Type'] = 'application/json;charset=utf-8';
    headers['Authorization'] = 'Bearer '+token;
    wx.request({
      url: API_BASE_URL + url,
      method: method,
      data: data,
      header: headers,
      success(res) {
         if(res.data.code == "401"){
            wx.redirectTo({
              url: '/pages/login/index',
            })
          }else{
            resolve(res.data)
          }
      },
      fail(error) {
        reject(error)
      }
    })
  })
}